package com.situ.stmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.stmall.entity.Shopcart;
import com.situ.stmall.mapper.ShopcartMapper;
import com.situ.stmall.service.ShopcartService;

@Service
public class ShopcartServiceImpl implements ShopcartService{

	@Autowired
	private ShopcartMapper shopcartMapper;
	
	@Override
	public int add(Shopcart cart) {
		//1、查找有没有对应的购物车信息
		Shopcart selectCart = shopcartMapper.select(cart);
		
		if (selectCart != null) {
			//在原来的基础上跟新
			selectCart.setCount(selectCart.getCount()+1);
			//更新到数据库
			return shopcartMapper.updateByPrimaryKey(selectCart);
		}else {
			//直接插入
			return shopcartMapper.insert(cart);
		}
		
		
		
	}

	@Override
	public List getByUserId(Integer userId) {
		return shopcartMapper.selectByUserId(userId);

}

	@Override
	public int update(Shopcart cart) {
		return shopcartMapper.updateByPrimaryKey(cart);
	}
}
